package com.ruoyi.api.activiti.service;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.ruoyi.activiti.service.IProcessService;
import com.ruoyi.api.activiti.domain.ExpenseReport;
import com.ruoyi.api.mqtt.MqttService;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.system.domain.SysUser;
import com.ruoyi.system.mapper.SysUserMapper;
import lombok.extern.slf4j.Slf4j;
import org.activiti.engine.HistoryService;
import org.activiti.engine.RuntimeService;
import org.activiti.engine.TaskService;
import org.activiti.engine.runtime.ProcessInstance;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

@Service
@Slf4j
public class ExpenseReportService extends UsualWrokService{
    @Value("${wspaas.dailyworkUrl}")
    private String dwUrl;
    @Autowired
    private RuntimeService runtimeService;
    @Autowired
    private TaskService taskService;
    @Autowired
    private HistoryService historyService;
    @Autowired
    private SysUserMapper userMapper;
    @Autowired
    private IProcessService processService;
    @Autowired
    private UsualWrokService usualWrokService;

    @Autowired
    private MqttService mqttService;
    public ProcessInstance submitApplyMobile(ExpenseReport expenseReport, String loginName, String key, HashMap<String, Object> variables,String service) {
        SysUser sysUser = userMapper.selectUserByLoginName(loginName);
        if (sysUser == null) {
            throw new RuntimeException("用户不存在");
        }
      JSONObject jsonObject = findById(expenseReport.getId(), service);
      ExpenseReport tmp =JSON.parseObject(jsonObject.toJSONString(),ExpenseReport.class);
        if(tmp == null){
          throw new RuntimeException("业务数据不存在");
        }
        expenseReport.setApplyUserName(sysUser.getUserName());
        expenseReport.setCreaterName(loginName);
        expenseReport.setApplyTime(DateUtils.getNowDate().getTime());
        String businessKey = expenseReport.getId().toString(); // 实体类 ID，作为流程的业务 key
        variables.put("applyUserId", loginName);
        variables.put("frontAssign", tmp.getAuditLoginName());
        variables.put("businessKey", businessKey);
        if(tmp.getProjectId() !=null)
          variables.put("projectId", tmp.getProjectId());
        ProcessInstance processInstance = processService.submitApply(loginName, businessKey, "申请", expenseReport.getReason(), key, variables);
        return processInstance;
    }

    /**
     * 撤销
     */
    public void cancelApply(String entityId) {
        ExpenseReport expenseReport = new ExpenseReport();
        expenseReport.setId(entityId);
        expenseReport.setSuspendState("3");
        expenseReport.setTaskName("已撤销");
        update(JSON.toJSONString(expenseReport), 1,"expense");
    }

    /**
     * instanceId：流程实例
     * loginNames：抄送人
     */
    public void addApplyCopy(String instanceId, String loginNameSender, String copyComment, String loginNameReceivers) {
        ProcessInstance processInstance = runtimeService.createProcessInstanceQuery().processInstanceId(instanceId).includeProcessVariables().singleResult();
        Map<String, Object> processVariables = processInstance.getProcessVariables();
        processVariables.put("copyReceivers", loginNameReceivers);
        processVariables.put("loginNameSender", loginNameSender);
        processVariables.put("copyComment", copyComment);
        runtimeService.setVariables(instanceId, processVariables);
    }
}
